
package com.lottery.webapp.action;

import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.action.ActionMessage;
import org.apache.struts.action.ActionMessages;

import com.lottery.webapp.action.BaseAction;
import com.lottery.Constants;
import com.lottery.model.Lottery539;
import com.lottery.service.Lottery539Manager;
import com.lottery.webapp.form.Lottery539Form;

/**
 * Action class to handle CRUD on a Lottery539 object
 *
 * @struts.action name="lottery539Form" path="/lottery539s" scope="request"
 *  validate="false" parameter="method" input="mainMenu"
 * @struts.action name="lottery539Form" path="/editLottery539" scope="request"
 *  validate="false" parameter="method" input="list"
 * @struts.action name="lottery539Form" path="/saveLottery539" scope="request"
 *  validate="true" parameter="method" input="edit"
 * @struts.action name="lottery539Form" path="/countLottery539" scope="request"
 *  validate="true" parameter="method" input="count"
 * @struts.action-set-property property="cancellable" value="true"
 * @struts.action-forward name="edit" path="/WEB-INF/pages/lottery539Form.jsp"
 * @struts.action-forward name="list" path="/WEB-INF/pages/lottery539List.jsp"
 * @struts.action-forward name="search" path="/lottery539s.html" redirect="true"
 * @struts.action-forward name="count" path="/WEB-INF/pages/count539Form.jsp"
 * @struts.action-forward name="countList" path="/WEB-INF/pages/count539List.jsp" 
 */
public final class Lottery539Action extends BaseAction {
    public ActionForward cancel(ActionMapping mapping, ActionForm form,
                                HttpServletRequest request,
                                HttpServletResponse response)
    throws Exception {
        return mapping.findForward("search");
    }

    public ActionForward delete(ActionMapping mapping, ActionForm form,
                                HttpServletRequest request,
                                HttpServletResponse response)
    throws Exception {
        if (log.isDebugEnabled()) {
            log.debug("Entering 'delete' method");
        }

        ActionMessages messages = new ActionMessages();
        Lottery539Form lottery539Form = (Lottery539Form) form;

        // Exceptions are caught by ActionExceptionHandler
        Lottery539Manager mgr = (Lottery539Manager) getBean("lottery539Manager");
        mgr.removeLottery539(lottery539Form.getId());

        messages.add(ActionMessages.GLOBAL_MESSAGE,
                     new ActionMessage("lottery539.deleted"));

        // save messages in session, so they'll survive the redirect
        saveMessages(request.getSession(), messages);

        return mapping.findForward("search");
    }

    public ActionForward edit(ActionMapping mapping, ActionForm form,
                              HttpServletRequest request,
                              HttpServletResponse response)
    throws Exception {
        if (log.isDebugEnabled()) {
            log.debug("Entering 'edit' method");
        }

        Lottery539Form lottery539Form = (Lottery539Form) form;

        // if an id is passed in, look up the user - otherwise
        // don't do anything - user is doing an add
        if (lottery539Form.getId() != null) {
            Lottery539Manager mgr = (Lottery539Manager) getBean("lottery539Manager");
            Lottery539 lottery539 = mgr.getLottery539(lottery539Form.getId());
            lottery539Form = (Lottery539Form) convert(lottery539);
            updateFormBean(mapping, request, lottery539Form);
        }

        return mapping.findForward("edit");
    }
    
    public ActionForward count(ActionMapping mapping, ActionForm form,
            HttpServletRequest request,
            HttpServletResponse response)
    throws Exception {
    	if (log.isDebugEnabled()) {
    		log.debug("Entering 'count' method");
    	}

    	Lottery539Form lottery539Form = (Lottery539Form) form;
    	
    	// if an id is passed in, look up the user - otherwise
    	// don't do anything - user is doing an add
    	if (lottery539Form.getId() != null) {
    		Lottery539Manager mgr = (Lottery539Manager) getBean("lottery539Manager");
    		Lottery539 lottery539 = mgr.getLottery539(lottery539Form.getId());
    		lottery539Form = (Lottery539Form) convert(lottery539);
    		updateFormBean(mapping, request, lottery539Form);
    	}

    	return mapping.findForward("count");
    }
    public ActionForward save(ActionMapping mapping, ActionForm form,
                              HttpServletRequest request,
                              HttpServletResponse response)
    throws Exception {
        if (log.isDebugEnabled()) {
            log.debug("Entering 'save' method");
        }

        // Extract attributes and parameters we will need
        ActionMessages messages = new ActionMessages();
        Lottery539Form lottery539Form = (Lottery539Form) form;
        boolean isNew = ("".equals(lottery539Form.getId()) || lottery539Form.getId() == null);

        Lottery539Manager mgr = (Lottery539Manager) getBean("lottery539Manager");
        Lottery539 lottery539 = (Lottery539) convert(lottery539Form);
        mgr.saveLottery539(lottery539);

        // add success messages
        if (isNew) {
            messages.add(ActionMessages.GLOBAL_MESSAGE,
                         new ActionMessage("lottery539.added"));

            // save messages in session to survive a redirect
            saveMessages(request.getSession(), messages);

            return mapping.findForward("search");
        } else {
            messages.add(ActionMessages.GLOBAL_MESSAGE,
                         new ActionMessage("lottery539.updated"));
            saveMessages(request, messages);

            return mapping.findForward("edit");
        }
    }

    public ActionForward search(ActionMapping mapping, ActionForm form,
                                HttpServletRequest request,
                                HttpServletResponse response)
    throws Exception {
        if (log.isDebugEnabled()) {
            log.debug("Entering 'search' method");
        }

        Lottery539Form lottery539Form = (Lottery539Form) form;
        Lottery539 lottery539 = (Lottery539) convert(lottery539Form);

        Lottery539Manager mgr = (Lottery539Manager) getBean("lottery539Manager");
        request.setAttribute(Constants.LOTTERY539_LIST, mgr.getLottery539s(lottery539));

        return mapping.findForward("list");
    }
    public ActionForward unspecified(ActionMapping mapping, ActionForm form,
                                     HttpServletRequest request,
                                     HttpServletResponse response)
            throws Exception {
        return search(mapping, form, request, response);
    }
    
    public ActionForward countList(ActionMapping mapping, ActionForm form,
            HttpServletRequest request,
            HttpServletResponse response)
    throws Exception {
    	if (log.isDebugEnabled()) {
    		log.debug("Entering 'countList' method");
    	}
    	int recNo=100;
    	if(request.getParameter("recNo")!=null )
    	{
    		recNo=Integer.parseInt(request.getParameter("recNo"));
    		log.debug("recNo:"+String.valueOf(recNo));
    	}

    	Lottery539Manager mgr = (Lottery539Manager) getBean("lottery539Manager");
    	List list= mgr.getLottery539s(new Lottery539());
    	
    	int lotto[]={0,0,0,0,0,0,0,0,0,0,
   			 			0,0,0,0,0,0,0,0,0,0,
   			 			0,0,0,0,0,0,0,0,0,0,
   			 			0,0,0,0,0,0,0,0,0,0,
   			 			0,0,0,0,0,0,0,0,0,0};
    	for(int i=0;i<recNo & i<list.size();i++)
    	{
    		Lottery539 l = (Lottery539)list.get(i);
    		lotto[Integer.parseInt(l.getNo1())] ++;
    		lotto[Integer.parseInt(l.getNo2())] ++;
    		lotto[Integer.parseInt(l.getNo3())] ++;
    		lotto[Integer.parseInt(l.getNo4())] ++;
    		lotto[Integer.parseInt(l.getNo5())] ++;
    	}
    	int max=0,min=0;
    	for(int i=0;i<50;i++)
    	{
    		max = Math.max(max,lotto[i]);
    		min = Math.min(min,lotto[i]);
    	}
    	request.getSession().setAttribute("max", String.valueOf(max));
    	request.getSession().setAttribute("min", String.valueOf(min));
    	request.getSession().setAttribute("lottery", lotto);

    	return mapping.findForward("countList");
    }    
}
